package com.zsp.coupon.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.http.protocol.RequestContent;


//通用的抽象过滤类
public abstract class AbstactZuulFilter extends ZuulFilter {

    RequestContext content;
        private  final static  String NEXT="next";

    //    用于过滤器之间传递消息，数据请求保持到每一个ThreadLocal中
    @Override
    public boolean shouldFilter(){
     RequestContext ctx=RequestContext.getCurrentContext();
     return (boolean) ctx.getOrDefault(NEXT,true);

    }

    @Override
    public  Object run() throws ZuulException {
     content=RequestContext.getCurrentContext();
     return cRun();

    }

    protected abstract Object cRun();
    Object fail(int code ,String msg){
        content.set(NEXT,false);
        content.setSendZuulResponse(false);
        content.getResponse().setContentType("text/html;chartset=UTF-8");
        content.setResponseStatusCode(code);
        content.setResponseBody(String.format("{\"result\":\"%s!\"}",msg));
        return null;
    }

    Object success() {

        content.set(NEXT, true);

        return null;
    }

}
